﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if XNA
using Microsoft.Xna.Framework;
#else
using SilverArcade.SilverSprite;
#endif
namespace CakeAnimation
{
    public class AnimationSequence
    {
        public List<Animation> AnimationsToPlay;
        public Vector2 Offset;
        public float CurrTime;
        
        int m_CurrAnim = 0;

        public AnimationSequence(List<Animation> anims)
        {
            CurrTime = 0.0f;
            AnimationsToPlay = anims;

        }

        public AnimationSequence()
        {
            CurrTime = 0.0f;
            AnimationsToPlay = new List<Animation>();

        }

        public void Update(float dt, Skeleton resultPose)
        {
            if (IsDone())
            {
                return;
            }

            Animation currAnimToPlay = AnimationsToPlay[m_CurrAnim];

            CurrTime += dt;
            if (CurrTime >= currAnimToPlay.GetDuration())
            {
                CurrTime = 0.0f;
                m_CurrAnim++;
                if (IsDone())
                {
                    return;
                }

                // Make sure new animation begins where the last one ended
                Offset = currAnimToPlay.GetLastKeyframe().Pose.RootBone.LocalPosition + Offset;

                currAnimToPlay = AnimationsToPlay[m_CurrAnim];

                // Make sure the animation "ignores" the first key frame's offet
                Offset -= currAnimToPlay.m_Keyframes[0].Pose.RootBone.LocalPosition;
            }


            if (currAnimToPlay.m_Keyframes.Count == 1)
            {
                currAnimToPlay.GetPoseAt(0.0f, resultPose);
                return;
            }


            int kfIndex = currAnimToPlay.GetKeyFrameBeforeTime(CurrTime);
            KeyFrame kf1 = currAnimToPlay.m_Keyframes[kfIndex];
            KeyFrame kf2 = currAnimToPlay.m_Keyframes[kfIndex + 1];

            float t = (CurrTime - kf1.Time) / (kf2.Time - kf1.Time);

            currAnimToPlay.GetPoseAt(CurrTime, resultPose);
            resultPose.RootBone.LocalPosition += Offset;
            resultPose.RootBone.UpdateTransform();
        }

        public void Reset()
        {
            Offset = Vector2.Zero;
            m_CurrAnim = 0;
        }

        public bool IsDone()
        {
            return m_CurrAnim == AnimationsToPlay.Count;
        }

        public Animation GetCurrentAnimation()
        {
            if (IsDone())
            {
                return null;
            }

            return AnimationsToPlay[m_CurrAnim];
        }

        public bool PlayingLastAnimation()
        {
            return m_CurrAnim == AnimationsToPlay.Count - 1;
        }

        public Animation GetLastAnimation()
        {
            if (AnimationsToPlay.Count == 0)
                return null;

            return AnimationsToPlay[AnimationsToPlay.Count - 1];
        }

        public Vector2 GetCurrentOffset()
        {
            if (IsDone())
            {
                return Vector2.Zero;
            }

            if (m_CurrAnim == 0)
            {
                return Vector2.Zero;
            }

            int anim = m_CurrAnim - 1;
            Vector2 offset = Vector2.Zero;
            while (anim >= 0)
            {
                offset += AnimationsToPlay[anim].GetLastKeyframe().Pose.RootBone.LocalPosition;
                --anim;
            }
            return offset;
        }

        public void AddAnimation(Animation anim)
        {

            if (AnimationsToPlay.Count > 0)
            {
                anim.m_Keyframes[0].Pose = GetLastAnimation().GetLastKeyframe().Pose;
            }
            
            AnimationsToPlay.Add(anim);
        }
    }
}
